package com.xzavier.yunlian_interview.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.xzavier.yunlian_interview.entity.User;
import com.xzavier.yunlian_interview.service.ex.BaseException;

public class JwtUtils {
    // 密钥字符串，应该存放在安全的地方
    private static final String SECRET_KEY = "Xzavier_interview";
    // 令牌过期时间设置，这里设置为 1 week
    private static final long EXPIRATION_TIME = 7 * 24 * 60 * 60 * 1000; // 2 hours

    public static String GetToken(User user) {
        String sign = JWT.create()
                .withSubject(user.getId())
                .sign(Algorithm.HMAC256(SECRET_KEY));
        return sign;
    }

    public static String verifyToken(String token) {
        try {
            String subject = JWT.require(Algorithm.HMAC256(SECRET_KEY))
                    .build()
                    .verify(token)
                    .getSubject();
            return subject;
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
            throw new BaseException("token失效");
        }
    }


}
